﻿using System;
using System.Collections;
using System.Web.Mvc;
using Huirui.Cavan.Application.MainBoundedContext.SalesModule;
using Huirui.Cavan.Domain.MainBoundedContext.SalesModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Presentation.ViewModel.SalesModule;
using Huirui.Cavan.Core.Extension;
using Huirui.Cavan.Domain.Seedwork;
using Newtonsoft.Json;
using System.Collections.Generic;
using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Application.MainBoundedContext.StockModule;
using System.Linq;
using Huirui.Cavan.Domain.MainBoundedContext.StockModule;

namespace Huirui.Cavan.Presentation.Controller.SalesModule
{
	#region Sale

    public class SalesController : EntityController<Sale, string, SaleSearch, ISalesViewModel, ISalesAppService>, ISalesController
	{
        public override string ControllerName
        {
            get { return ControllerConst.SALES; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public SalesController()
        {
            this.Model = ModelFactory.CreateModel<ISalesViewModel>();
        }

        [ActionName(ActionConst.ADD)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "sales_query")]
        public ActionResult AddSale(string id)
        {
            if (id.IsNotEmpty())
            {
                this.Model.Item = this.AppService.GetById(id);
            }
            else
            {
                this.Model.Item = new Sale();
                this.Model.Item.SalesDate = DateTime.Now;
                this.Model.Item.DeadDate = DateTime.Now;
                this.Model.Item.Id = "";
            }

            return View("~/Views/Sales/v1/addSales.cshtml", this.Model);
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "sales_edit")]
        public JsonResult AddSale(SalesViewModel model)
        {
            BoolResult jsonResult = null;
            BoolGenericResult<Sale> result = null;
            model.Item.StockInOut = new StockInOut();
            IList<StockInOutDetail> stockInOutDetails = JsonConvert.DeserializeObject<IList<StockInOutDetail>>(model.StockInOutDetails);
            IList<SalesOrder> salesOrders = JsonConvert.DeserializeObject<IList<SalesOrder>>(model.SalesOrders);

            if (salesOrders.IsNotNull() && salesOrders.Count > 0)
            {
                foreach (SalesOrder so in salesOrders)
                {
                    model.Item.StockInOut.StockInOutDetails = stockInOutDetails.Where(s => s.Description == so.Id).ToList();
                    model.Item.SalesOrder = so;
                    result = this.AppService.AddOrUpdate(model.Item);
                    
                }
            }
            else
            {
                model.Item.StockInOut.StockInOutDetails = stockInOutDetails;
                result = this.AppService.AddOrUpdate(model.Item);
            }
            jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }
    }
	#endregion
}